package com.atguigu.srb.core.controller.admin;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.atguigu.srb.base.result.R;
import com.atguigu.srb.core.listener.DictExcelDataListener;
import com.atguigu.srb.core.mapper.DictMapper;
import com.atguigu.srb.core.pojo.data.DictData;
import com.atguigu.srb.core.pojo.entity.Dict;
import com.atguigu.srb.core.service.DictService;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典管理 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-01-04
 */
@RestController
//@CrossOrigin
@RequestMapping("/admin/core/dict")
public class AdminDictController {
    @Resource
    DictService dictService;

    @ApiOperation("根据id删除数据字典")
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id")String id){
        //判断如果没有下一级数据删除数据字典
        dictService.deleteById(id);
        return R.ok();
    }

    @ApiOperation("根据parentId查询一组数据字典")
    @GetMapping("dictsByParentId/{pid}")
    public R getDictsByParentId(@PathVariable("pid")String pid){
        List<Dict> dicts = dictService.getDictsByParentId(pid);
        return R.ok().data("items" , dicts);
    }
    @ApiOperation("导出数据字典")
    @GetMapping("export")
    //导出的是excel文件,excel文件可以使用response对象设置到响应报文响应体中
    public void exportDicts(HttpServletResponse response){
        dictService.exportDicts(response);
    }

    //接收上传的数据字典excel文件，解析导入数据到数据库数据字典表中
    @PostMapping("import")
    public R importDicts(MultipartFile excel){//MultipartFile 接收上传的文件的对象
        //处理文件上传的业务
        dictService.importDicts(excel);
        return  R.ok();
    }

}

